package cn.imvc.app.controllers.goods;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsAttrValue;
import cn.imvc.app.entities.GoodsSpec;
import cn.imvc.app.models.GoodsModel;
import cn.imvc.app.models.GoodsSkuModel;
import cn.imvc.app.models.GoodsSpecModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

//TODO zhanghy
public class GoodsSpecControllerImpl implements GoodsSpecController{

	@Autowired
	GoodsSpecModel goodsSpecModel;
	
	public String goodsId;
	public Page<GoodsSpec> page;
	public int id;
	public int[] ids;
	public GoodsSpec goodsSpec;
	public ViewRequest viewRequest;
	
	
	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public void setGoodsSpec(GoodsSpec goodsSpec) {
		this.goodsSpec = goodsSpec;
	}

	public String search() {
		// TODO Auto-generated method stub
		return null;
	}

	public String index() {
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("goodsId_eq_string", String.valueOf(goodsId));
		viewRequest = new ViewRequest();
		viewRequest.setParams(searchMap);
		page=goodsSpecModel.find(viewRequest);
		return "goodsSpec/index";
	}

	public String show() {
		// TODO Auto-generated method stub
		return null;
	}

	public String editNew() {
		goodsSpec = new GoodsSpec();
		Goods goods = new Goods();
		goods.setId(Integer.valueOf(goodsId));
		goodsSpec.setGoods(goods);
		return "goodsSpec/edit";
	}

	

	public String create() {
//		if(goodsAttrValue.getAttrValue() != null && goodsAttrValue.getAttrValue().getId() == 0){
//			goodsAttrValue.setAttrValue(null);
//		}
		goodsSpec = goodsSpecModel.save(goodsSpec);
		if(goodsSpec != null){
			return Render.json(new DialogBean("200", "创建成功","goods_spec_class", "closeCurrent"));
		}else{
			return Render.json(new DialogBean("200", "已存在该规格类别，不能添加","", ""));
		}
	}

	public String edit() {
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("goodsId_eq_string", String.valueOf(id));
		viewRequest = new ViewRequest();
		viewRequest.setParams(searchMap);
		page=goodsSpecModel.find(viewRequest);
		return "goodsSpec/edit";
	}

	public String update() {
		goodsSpecModel.update(goodsSpec);
		return Render.json(new DialogBean("200", "更新成功","goods_spec_class", "closeCurrent"));
	}

	public String destroy() {
		String str = goodsSpecModel.delete(id);
		if(str.equals("")){
			return Render.json(new DialogBean("200", "删除成功","goods_spec_class"));
		}else{
			return Render.json(new DialogBean("300", str,"goods_spec_class"));
		}
	}

	public String destroyBatch() {
		String str = goodsSpecModel.deleteBatch(ids);
		if(str.equals("")){
			return Render.json(new DialogBean("200", "删除成功","goods_spec_class"));
		}else{
			return Render.json(new DialogBean("300", str,"goods_spec_class"));
		}
	}

}
